package com.mini.wechat.miniwechat.schedule;

import com.alibaba.fastjson.JSONObject;
import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.mini.wechat.miniwechat.utils.HttpUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;


/**
 * @program: imall-service
 * @description: 定时更新优惠券商品并同步到数据库
 * @author: 王学伟
 * @create: 2018-11-12 10:20
 **/
@Component
@Async
public class WechatScheduled {
	private final Logger logger = LoggerFactory.getLogger(this.getClass());
	private final String WEATHER_ULR ="http://t.weather.sojson.com/api/weather/city/101030100";
	private final String CAR_URl = "https://api.jisuapi.com/vehiclelimit/query?appkey=281226e9203855a9&city=tianjin&date=";
	Cache<String, Object> manualCache = Caffeine.newBuilder().build();


	@Scheduled(cron = "0 0 6 * * ?")
	public void check(){
		logger.info("====开始更新缓存====");
		manualCache.put("m_is_use",0);
		manualCache.put("n_is_use",0);
		JSONObject jsonWea = HttpUtils.getHttpToJSONObject(WEATHER_ULR);
		String dateStr = new SimpleDateFormat("yyyy-MM-dd").format(new Date()).toString();
		JSONObject jsonCar = HttpUtils.getHttpToJSONObject(CAR_URl+dateStr);
		manualCache.put("wea_info",jsonWea.toString());
		manualCache.put("car_info",jsonCar.toString());
		logger.info("====缓存更新完毕====");

	}
}
